using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Globalization;

namespace IPConfigLibrary
{
    public class IPConfig
    {
        #region Members
        private string _HostName = String.Empty;
        private string _PrimaryDNSSuffix = String.Empty;
        private string _NodeType = String.Empty;
        private bool _IPRoutingEnabled;
        private bool _WINSProxyEnabled;
        private string _DNSSuffixSearchList = String.Empty;
        private List<Adaptor> _Adaptors = new List<Adaptor>();
        #endregion

        #region Properties
        public string HostName
        {
            get { return _HostName; }
            set { _HostName = value; }
        }

        public string PrimaryDNSSuffix
        {
            get { return _PrimaryDNSSuffix; }
            set { _PrimaryDNSSuffix = value; }
        }

        public string NodeType
        {
            get { return _NodeType; }
            set { _NodeType = value; }
        }

        public bool IPRoutingEnabled
        {
            get { return _IPRoutingEnabled; }
            set { _IPRoutingEnabled = value; }
        }

        public bool WINSProxyEnabled
        {
            get { return _WINSProxyEnabled; }
            set { _WINSProxyEnabled = value; }
        }

        public string DNSSuffixSearchList
        {
            get { return _DNSSuffixSearchList; }
            set { _DNSSuffixSearchList = value; }
        }

        public List<Adaptor> Adaptors
        {
            get { return _Adaptors; }
        }
        #endregion

        #region Methods
        #region Constructors
        public IPConfig() { }

        public IPConfig(
            string theHostName,
            string thePrimaryDNSSuffix,
            string theNodeType,
            bool theIPRoutingEnabled,
            bool theWINSProxyEnabled,
            string theDNSSuffixSearchList)
        {
            this._HostName = theHostName;
            this._PrimaryDNSSuffix = thePrimaryDNSSuffix;
            this._NodeType = theNodeType;
            this._IPRoutingEnabled = theIPRoutingEnabled;
            this._WINSProxyEnabled = theWINSProxyEnabled;
            this._DNSSuffixSearchList = theDNSSuffixSearchList;
        }
        #endregion

        public static IPConfig Initialise()
        {
            IPConfig theConfig = new IPConfig();

            Process myProcess = new Process();
            ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("IPCONFIG.exe");
            myProcessStartInfo.UseShellExecute = false;
            myProcessStartInfo.RedirectStandardOutput = true;
            myProcessStartInfo.Arguments = "/all";
            myProcessStartInfo.CreateNoWindow = true;
            myProcess.StartInfo = myProcessStartInfo;
            myProcess.Start();

            StreamReader myStreamReader = myProcess.StandardOutput;

            int theCurrentAdaptor = 0;

            while (!myProcess.StandardOutput.EndOfStream)
            {
                string thisLine = myProcess.StandardOutput.ReadLine().Trim();
                thisLine = thisLine.Replace("\r", "");
                thisLine = thisLine.Replace("\n", "");
                if (!String.IsNullOrEmpty(thisLine))
                {
                    string theValue = 
                        thisLine.LastIndexOf(": ").Equals(-1)
                            ? String.Empty
                            : thisLine.Substring(thisLine.LastIndexOf(": ") + 2);
                    #region Windows IP Configuration
                    if (thisLine.Contains("Host Name"))
                    {
                        theConfig.HostName = theValue;
                    }
                    if (thisLine.Contains("Primary Dns Suffix"))
                    {
                        theConfig.PrimaryDNSSuffix = theValue;
                    }
                    if (thisLine.Contains("Node Type"))
                    {
                        theConfig.NodeType = theValue;
                    }
                    if (thisLine.Contains("IP Routing Enabled"))
                    {
                        theConfig.IPRoutingEnabled = theValue.Equals("Yes");
                    }
                    if (thisLine.Contains("WINS Proxy Enabled"))
                    {
                        theConfig.WINSProxyEnabled = theValue.Equals("Yes");
                    }
                    if (thisLine.Contains("DNS Suffix Search List"))
                    {
                        theConfig.DNSSuffixSearchList = theValue;
                    }
                    #endregion

                    #region Adaptors
                    if (thisLine.Contains("Connection-specific DNS Suffix"))
                    {
                        theConfig.AddAdaptor(new Adaptor());
                        theCurrentAdaptor = theConfig.Adaptors.Count - 1;
                        theConfig.Adaptors[theCurrentAdaptor].ConnectionSpecificDNSSuffix = theValue;
                    }
                    if (thisLine.Contains("Description"))
                    {
                        theConfig.Adaptors[theCurrentAdaptor].Description = theValue;
                    }
                    if (thisLine.Contains("Physical Address"))
                    {
                        theConfig.Adaptors[theCurrentAdaptor].PhysicalAddress = theValue;
                    }
                    if (thisLine.Contains("Dhcp Enabled"))
                    {
                        theConfig.Adaptors[theCurrentAdaptor].DHCPEnabled = theValue.Equals("Yes");
                    }
                    if (thisLine.Contains("Autoconfiguration Enabled"))
                    {
                        theConfig.Adaptors[theCurrentAdaptor].AutoConfigurationEnabled = theValue.Equals("Yes");
                    }
                    if (thisLine.Contains("IP Address"))
                    {
                        if (!String.IsNullOrEmpty(theValue))
                        {
                            theConfig.Adaptors[theCurrentAdaptor].IPAddress = IPAddress.Parse(theValue);
                        }
                    }
                    if (thisLine.Contains("Subnet Mask"))
                    {
                        if (!String.IsNullOrEmpty(theValue))
                        {
                            theConfig.Adaptors[theCurrentAdaptor].SubnetMask = IPAddress.Parse(theValue);
                        }
                    }
                    if (thisLine.Contains("Default Gateway"))
                    {
                        if (!String.IsNullOrEmpty(theValue))
                        {
                            theConfig.Adaptors[theCurrentAdaptor].DefaultGateway = IPAddress.Parse(theValue);
                        }
                    }
                    if (thisLine.Contains("DHCP Server"))
                    {
                        if (!String.IsNullOrEmpty(theValue))
                        {
                            theConfig.Adaptors[theCurrentAdaptor].DHCPServer = IPAddress.Parse(theValue);
                        }
                    }
                    if (thisLine.Contains("DNS Servers"))
                    {
                        if (!String.IsNullOrEmpty(theValue))
                        {
                            theConfig.Adaptors[theCurrentAdaptor].DNSServer = IPAddress.Parse(theValue);
                        }
                    }
                    if (thisLine.Contains("Lease Obtained"))
                    {
                        theConfig.Adaptors[theCurrentAdaptor].LeaseObtained =
                            DateTime.Parse(theValue, CultureInfo.CurrentCulture);
                    }
                    if (thisLine.Contains("Lease Expires"))
                    {
                        theConfig.Adaptors[theCurrentAdaptor].LeaseExpires =
                            DateTime.Parse(theValue, CultureInfo.CurrentCulture);
                    }
                    #endregion
                }
            }
            myProcess.WaitForExit();

            myProcess.Close();

            return theConfig;
        }

        public static IPConfig Release()
        {
            Process myProcess = new Process();
            ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("IPCONFIG.exe");
            myProcessStartInfo.UseShellExecute = false;
            myProcessStartInfo.RedirectStandardOutput = true;
            myProcessStartInfo.Arguments = "/release";
            myProcessStartInfo.CreateNoWindow = true;
            myProcess.StartInfo = myProcessStartInfo;
            myProcess.Start();

            myProcess.WaitForExit();

            myProcess.Close();

            return IPConfig.Initialise();
        }

        public static IPConfig Renew()
        {
            Process myProcess = new Process();
            ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("IPCONFIG.exe");
            myProcessStartInfo.UseShellExecute = false;
            myProcessStartInfo.RedirectStandardOutput = true;
            myProcessStartInfo.Arguments = "/renew";
            myProcessStartInfo.CreateNoWindow = true;
            myProcess.StartInfo = myProcessStartInfo;
            myProcess.Start();

            myProcess.WaitForExit();

            myProcess.Close();

            return IPConfig.Initialise();
        }
        public void AddAdaptor(Adaptor theAdaptor)
        {
            this._Adaptors.Add(theAdaptor);
        }
        #endregion
    }
}
